#include <cstdio>
#include <algorithm>
using namespace std;

int a[509] = {0};
int b[509] = {0};
int h[509] = {0};

bool cmp(int x, int y) {
	return x > y;
}

void runCase()
{
	int n = 0, m = 0;
	scanf("%d %d", &n, &m);
	for ( int i = 0; i < n; i++ ) {
		scanf("%d %d", &a[i], &b[i]);
		h[i] = b[i] - a[i];
	}
	sort(h, h+n, cmp);
	long long tot = 0;
	int len = min(n, m);
	for ( int i = 0; i < len; i++ ) {
		if ( h[i] <= 0 ) {
			break;
		}
		tot += h[i];
	}
	for ( int i = 0; i < n; i++ ) {
		tot += a[i];
	}
	printf("%lld\n", tot);
}

int main()
{
	freopen("sprite.in", "r", stdin);
	freopen("sprite.out", "w", stdout);
	int t = 0;
	scanf("%d", &t);
	for ( int i = 0; i < t; i++ ) {
		runCase();
	}
} 
